﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace FishScore
{
    /// <summary>
    /// 钓手信息
    /// </summary>
    public class Player
    {
        /// <summary>
        /// 报名号
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 手机号
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 哪个团队
        /// </summary>
        public string Team { get; set; }
    }

    /// <summary>
    /// 场次信息
    /// </summary>
    public class Match
    {
        /// <summary>
        /// 场次号
        /// </summary>
        public int MatchId { get; set; }

        /// <summary>
        /// 开始钓位
        /// </summary>
        public int StartPos { get; set; }

        /// <summary>
        /// 结束钓位
        /// </summary>
        public int EndPos { get; set; }

        /// <summary>
        /// 间隔人数
        /// </summary>
        public  int SeprateNumber {  get; set; }
    }

    /// <summary>
    /// 每场的钓鱼成绩及分数
    /// </summary>
    public class Fish
    {
        /// <summary>
        /// 场次号
        /// </summary>
        public int MatchId { get; set; }

        /// <summary>
        /// 区号
        /// </summary>
        public int AreaId { get; set; }

        /// <summary>
        /// 钓位
        /// </summary>
        public int AreaPos { get; set; }

        /// <summary>
        /// 哪个钓手
        /// </summary>
        public int PlayerId { get; set; }

        /// <summary>
        /// 尾数
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 重量
        /// </summary>
        public float Weight { get; set; }

        /// <summary>
        /// 分区成绩
        /// </summary>
        public float AreaScore { get; set; }
    }

    /// <summary>
    /// 分数信息
    /// </summary>
    public class Score
    {
        /// <summary>
        /// 报名号
        /// </summary>
        public int PlayerId { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 排名
        /// </summary>
        public int Rank { get; set; }

        /// <summary>
        /// 总分
        /// </summary>
        public float TotalScore { get; set; }

        /// <summary>
        /// 第一场成绩
        /// </summary>
        public float Fish1 { get; set; }

        /// <summary>
        /// 第一场分数
        /// </summary>
        public float Score1 { get; set; }

        /// <summary>
        /// 第一场区号
        /// </summary>
        public int AreaId1 { get; set; }

        /// <summary>
        /// 第二场分数
        /// </summary>
        public float Fish2 { get; set; }

        /// <summary>
        /// 第二场分数
        /// </summary>
        public float Score2 { get; set; }

        /// <summary>
        /// 第二场区号
        /// </summary>
        public int AreaId2 { get; set; }

        /// <summary>
        /// 第三场分数
        /// </summary>
        public float Fish3 { get; set; }

        /// <summary>
        /// 第三场分数
        /// </summary>
        public float Score3 { get; set; }

        /// <summary>
        /// 第三场区号
        /// </summary>
        public int AreaId3 { get; set; }

        /// <summary>
        /// 第四场分数
        /// </summary>
        public float Fish4 { get; set; }

        /// <summary>
        /// 第四场分数
        /// </summary>
        public float Score4 { get; set; }

        /// <summary>
        /// 第四场区号
        /// </summary>
        public int AreaId4 { get; set; }

        /// <summary>
        /// 哪个团队
        /// </summary>
        public string Team { get; set; }
    }

    /// <summary>
    /// 团队成绩及排名信息
    /// </summary>
    public class TeamRank
    {
        /// <summary>
        /// 团队名称
        /// </summary>
        public string TeamName { get; set; }

        /// <summary>
        /// 排名
        /// </summary>
        public int Rank { get; set; }

        /// <summary>
        /// 团队总分
        /// </summary>
        public float TeamScore { get; set; }

        /// <summary>
        /// 第一位钓手姓名
        /// </summary>
        public string Play1Name { get; set; }

        /// <summary>
        /// 第一位钓手分数
        /// </summary>
        public float Play1Score { get; set; }

        /// <summary>
        /// 第二位钓手姓名
        /// </summary>
        public string Play2Name { get; set; }

        /// <summary>
        /// 第二位钓手分数
        /// </summary>
        public float Play2Score { get; set; }

        /// <summary>
        /// 第三位钓手姓名
        /// </summary>
        public string Play3Name { get; set; }

        /// <summary>
        /// 第三位钓手分数
        /// </summary>
        public float Play3Score { get; set; }

        /// <summary>
        /// 第四位钓手姓名
        /// </summary>
        public string Play4Name { get; set; }

        /// <summary>
        /// 第四位钓手分数
        /// </summary>
        public float Play4Score { get; set; }
    }

    /// <summary>
    /// 违规加分
    /// </summary>
    public class AddScore
    {
        public int PlayerId { get; set; }
        public int MatchId { get; set; }
        public int Score { get; set; }

        public override string ToString()
        {
            return string.Format("{0},{1},{2}", PlayerId, MatchId, Score);
        }
    }

    public class FishGameObject
    {
        /// <summary>
        /// 赛事名称
        /// </summary>
        public string GameName { get; set; }

        /// <summary>
        /// 比赛日期
        /// </summary>
        public string Date { get; set; }

        /// <summary>
        /// 多少个区
        /// </summary>
        public int AreaCount { get; set; }

        /// <summary>
        /// 每个区多少个人
        /// </summary>
        public int AreaPlayers { get; set; }

        /// <summary>
        /// 每区开始钓位号
        /// </summary>
        public int AreaStartPos { get; set; }
        /// <summary>
        /// 间隔人数
        /// </summary>
        public int SeprateNumber { get; set; }

        /// <summary>
        /// 场次列表
        /// </summary>
        public List<Match> Matchs { get; set; }

        /// <summary>
        /// 钓手列表
        /// </summary>
        public List<Player> Players { get; set; }

        /// <summary>
        /// 每个钓手每场比赛的成绩和分数
        /// </summary>
        public List<Fish> Fishs { get; set; }

        //违规加分清单
        public List<AddScore> AddScores { get; set; }

        public FishGameObject()
        {
        }

        public void InitEmptyObject()
        {
            AreaCount = 8;
            AreaPlayers = 12;
            AreaStartPos = 1;
            Date = DateTime.Now.ToString("D");
            Matchs = new List<Match>{
                new Match {MatchId = 1, StartPos = AreaStartPos, EndPos = AreaPlayers + AreaStartPos - 1},
                new Match {MatchId = 2, StartPos = AreaStartPos, EndPos = AreaPlayers + AreaStartPos - 1},
                new Match {MatchId = 3, StartPos = AreaStartPos, EndPos = AreaPlayers + AreaStartPos - 1},
                new Match {MatchId = 4, StartPos = AreaStartPos, EndPos = AreaPlayers + AreaStartPos - 1}};

            Fishs = new List<Fish>();
            Players = new List<Player>();
            AddScores = new List<AddScore>();

            GameName = "新的比赛";
        }

        public Player GetPlayerByID(int id)
        {
            if (id <= 0) return null;

            return Players.Where(x => x.ID == id).FirstOrDefault();
        }

        public Match GetMatchById(int id)
        {
            return Matchs.Where(x => x.MatchId == id).FirstOrDefault();
        }

        public List<Fish> GetFishByMatch(int mid)
        {
            return Fishs.Where(x => x.MatchId == mid).ToList();
        }

        public List<Fish> GetFishByMatchAndArea(int mid, int areaid)
        {
            return Fishs.Where(x => x.MatchId == mid && x.AreaId == areaid).ToList();
        }

        public List<Fish> GetFishByPlayerId(int pid)
        {
            return Fishs.Where(x => x.PlayerId == pid).ToList();
        }

        public Fish GetFishByMatchAndAreaAndPlayer(int mid, int aid, int playerID)
        {
            return Fishs.Where(x => x.MatchId == mid && x.AreaId == aid && x.PlayerId == playerID).FirstOrDefault();
        }

        public Fish GetFishByMatchAndPlayer(int mid, int pid)
        {
            return Fishs.Where(x => x.MatchId == mid && x.PlayerId == pid).FirstOrDefault();
        }

        public Fish GetFishByMatchAndAreaAndPos(int mid, int aid, int pid)
        {
            return Fishs.Where(x => x.MatchId == mid && x.AreaId == aid && x.AreaPos == pid).FirstOrDefault();
        }

        public List<AddScore> GetAddScoreByPlayerId(int pid)
        {
            return AddScores.Where(x => x.PlayerId == pid).ToList();
        }

        public List<Score> InitScore()
        {
            //默认每场分数100，为了中途离场人员的排名靠后
            return Players.Select(x => new Score
            {
                PlayerId = x.ID,
                Name = x.Name,
                Team = x.Team,
                Score1 = 100,
                Score2 = 100,
                Score3 = 100,
                Score4 = 100
            }).ToList();
        }

        public List<TeamRank> InitTeam()
        {
            // 获取所有不重复的团队名称数组
            string[] teams = Players.Select(x => x.Team).Distinct().ToArray();

            // 创建团队排名列表
            List<TeamRank> teamList = new List<TeamRank>();
            foreach (string team in teams)
            {
                // 跳过空球队名称
                if (string.IsNullOrEmpty(team)) continue;

                // 添加新团队排名对象到列表
                teamList.Add(new TeamRank { TeamName = team });
            }
            return teamList;
        }

        public void FillScores(ref List<Score> list)
        {
            foreach (Fish fish in Fishs)
            {
                float value, score;

                value = fish.AreaId < 5 ? fish.Weight : fish.Count;

                score = fish.AreaScore;

                Score s = list.Where(x => x.PlayerId == fish.PlayerId).FirstOrDefault();
                switch (fish.MatchId)
                {
                    case 1:
                        s.Fish1 = value;
                        s.Score1 = score;
                        s.AreaId1 = fish.AreaId;
                        break;

                    case 2:
                        s.Fish2 = value;
                        s.Score2 = score;
                        s.AreaId2 = fish.AreaId;
                        break;

                    case 3:
                        s.Fish3 = value;
                        s.Score3 = score;
                        s.AreaId3 = fish.AreaId;
                        break;

                    case 4:
                        s.Fish4 = value;
                        s.Score4 = score;
                        s.AreaId4 = fish.AreaId;
                        break;
                }
            }
        }

        public void CalculateScores2(ref List<Score> list, bool filterNotAll)
        {
            FillScores(ref list);

            List<Score> list14 = list.Where(x => x.AreaId1 <= 4).ToList();
            list14.ForEach(x => x.TotalScore = x.Score1 + x.Score2);
            List<Score> list58 = list.Where(x => x.AreaId1 > 4).ToList();
            list58.ForEach(x => x.TotalScore = x.Score1 + x.Score2);

            list14.Sort(new ScoreCompare(2));
            list58.Sort(new ScoreCompare(2));

            //更新排名字段
            for (int i = 0; i < list14.Count; i++)
            {
                list14[i].Rank = i + 1;
            }

            //更新排名字段
            for (int i = 0; i < list58.Count; i++)
            {
                list58[i].Rank = i + 1;
            }

            list = list14.Concat(list58).ToList();
        }

        //根据选手编号，获取每次违规加分的总和
        public int GetSumAddScore(int playerID)
        {
            return AddScores.Where(x => x.PlayerId == playerID).Sum(x => x.Score);
        }

        public void CalculateScores(ref List<Score> list, bool filterNotAll)
        {
            FillScores(ref list);
            //计算总分
            //每场比赛都初始化最高分了，不需要判断是否0，不需要Select了，通过ForEach修改总分即可
            list.ForEach(x => x.TotalScore = x.Score1 + x.Score2 + x.Score3 + x.Score4 + GetSumAddScore(x.PlayerId));

            //过滤中途退赛或者未参加的人员(成绩少于4场的无效)
            if (filterNotAll)
                list.RemoveAll(x => Fishs.Count(y => y.PlayerId == x.PlayerId) != 4);

            list.Sort(new ScoreCompare(4));

            //更新排名字段
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Rank = i + 1;
            }
        }

        /// <summary>
        /// 根据成绩计算分区分数,密集排序法
        /// </summary>
        /// <param name="scores"></param>
        /// <returns></returns>
        public static float[] GetAverageRanks(float[] scores)
        {
            if (scores == null || scores.Length == 0)
            {
                return new float[0];
            }

            // 降序排序
            var sortedScores = scores.OrderByDescending(x => x).ToList();

            // 记录每个分数对应的所有位置（从1开始）
            Dictionary<float, List<int>> scorePositions = new Dictionary<float, List<int>>();
            for (int i = 0; i < sortedScores.Count; i++)
            {
                float currentScore = sortedScores[i];
                int position = i + 1;

                if (!scorePositions.ContainsKey(currentScore))
                {
                    scorePositions[currentScore] = new List<int>();
                }
                scorePositions[currentScore].Add(position);
            }

            // 计算每个分数的平均排名
            Dictionary<float, float> rankMap = new Dictionary<float, float>();
            foreach (var pair in scorePositions)
            {
                float averageRank = (float)pair.Value.Average();
                rankMap[pair.Key] = averageRank;
            }

            // 构建结果数组
            float[] result = new float[scores.Length];
            for (int i = 0; i < scores.Length; i++)
            {
                result[i] = rankMap[scores[i]];
            }

            return result;
        }
    }

    //自定义排序类(针对大小鱼分开计分的排序算法)
    /*public class ScoreCompare2 : IComparer<Score>
    {
        private List<Fish> fishList;
        private bool IsHunYang = false;
        public ScoreCompare2(List<Fish> fishes, bool HunYang)
        {
            fishList = fishes;
            IsHunYang = HunYang;
        }

        private int CompareArray(float[] a, float[] b)
        {
            int i = a.Length;
            for (int j = 0; j < i; j++)
            {
                if (a[j] > b[j])
                    return 1;
                else if (a[j] < b[j])
                    return -1;
            }
            return 0;
        }

        private int CompareArray2(float[] a, float[] b)
        {
            int i = a.Length;
            for (int j = 0; j < i; j++)
            {
                if (a[j] > b[j])
                    return -1;
                else if (a[j] < b[j])
                    return 1;
            }
            return 0;
        }

        private int CompareByPlayerId(int xpid, int ypid)
        {
            List<Fish> xList = fishList.Where(x => x.playerId == xpid).ToList();
            List<Fish> yList = fishList.Where(x => x.playerId == ypid).ToList();

            //没有2场分数就相同的不比了
            //if ((xList.Count != 2) || (yList.Count != 2))
            //    return 0;

            if (IsHunYang)
            {
                //如果是混养，获取2场分数，并且各自排序好后合并为一个数组
                var xxfValues = xList.Where(x=> x.areaId <= 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
                var xValues = xxfValues.ToArray();

                var yxfValues = yList.Where(x => x.areaId <= 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
                var yValues = yxfValues.ToArray();

                //对分数数组进行逐个比较返回结果,小的在前面
                int ret = CompareArray(xValues, yValues);
                if (ret != 0)
                    return ret;

                //获取2场成绩，混养鱼重量，并且各自排序好后合并为一个数组
                var xxfCounts = xList.Where(x => x.areaId <= 4).OrderByDescending(o => o.weight).Select(t => t.weight).ToArray();
                var xCounts = xxfCounts.ToArray();

                var yxfCounts = yList.Where(x => x.areaId <= 4).OrderByDescending(o => o.weight).Select(t => t.weight).ToArray();
                var yCounts = yxfCounts.ToArray();

                //对成绩(对象鱼数量,混养鱼重量)数组进行逐个比较返回结果,大的在前面
                ret = CompareArray2(xCounts, yCounts);

                return ret;
            }
            else
            {
                //如果是对象鱼，获取2场分数，并且各自排序好后合并为一个数组
                var xxfValues = xList.Where(x => x.areaId > 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
                var xValues = xxfValues.ToArray();

                var yxfValues = yList.Where(x => x.areaId > 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
                var yValues = yxfValues.ToArray();

                //对分数数组进行逐个比较返回结果,小的在前面
                int ret = CompareArray(xValues, yValues);
                if (ret != 0)
                    return ret;

                //获取2场成绩，对象鱼数量，并且各自排序好后合并为一个数组
                var xxfCounts = xList.Where(x => x.areaId > 4).OrderByDescending(o => o.count).Select(t => (float)t.count).ToArray();
                var xCounts = xxfCounts.ToArray();

                var yxfCounts = yList.Where(x => x.areaId > 4).OrderByDescending(o => o.count).Select(t => (float)t.count).ToArray();
                var yCounts = yxfCounts.ToArray();

                //对成绩(对象鱼数量)数组进行逐个比较返回结果,大的在前面
                ret = CompareArray2(xCounts, yCounts);

                return ret;
            }
        }
        public int Compare(Score x, Score y)
        {
            if (x.TotalScore > y.TotalScore)
            {
                return 1;
            }
            else if (x.TotalScore < y.TotalScore)
            {
                return -1;
            }
            else//如果总分相同，则比较对象鱼分数，混养鱼分数，对象鱼数量及混养鱼重量。
            {
                return CompareByPlayerId(x.playerId, y.playerId);
            }
        }
    }

    //自定义排序类
    public class ScoreCompare : IComparer<Score>
    {
        private List<Fish> fishList;
        public ScoreCompare(List<Fish> fishes)
        {
            fishList = fishes;
        }

        private int CompareArray(float[] a, float[] b)
        {
            int i = a.Length;
            for (int j = 0; j < i; j++)
            {
                if (a[j] > b[j])
                    return 1;
                else if (a[j] < b[j])
                    return -1;
            }
            return 0;
        }

        private int CompareArray2(float[] a, float[] b)
        {
            int i = a.Length;
            for (int j = 0; j < i; j++)
            {
                if (a[j] > b[j])
                    return -1;
                else if (a[j] < b[j])
                    return 1;
            }
            return 0;
        }

        private int CompareByPlayerId(int xpid, int ypid)
        {
            List<Fish> xList = fishList.Where(x => x.playerId == xpid).ToList();
            List<Fish> yList = fishList.Where(x => x.playerId == ypid).ToList();

            //没有4场分数就相同的不比了
            if ((xList.Count != 4) || (yList.Count != 4))
                return 0;

            //获取4场分数，前面2个是对象鱼，后面2个是混养鱼，并且各自排序好后合并为一个数组
            var xxfValues = xList.Where(p => p.areaId > 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
            var xdfValues = xList.Where(p => p.areaId <= 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
            var xValues = xxfValues.Concat(xdfValues).ToArray();

            var yxfValues = yList.Where(p => p.areaId > 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
            var ydfValues = yList.Where(p => p.areaId <= 4).OrderBy(o => o.areaScore).Select(t => t.areaScore).ToArray();
            var yValues = yxfValues.Concat(ydfValues).ToArray();

            //对分数数组进行逐个比较返回结果,小的在前面
            int ret = CompareArray(xValues, yValues);
            if (ret != 0)
                return ret;

            //获取4场成绩，前面2个是对象鱼数量，后面2个是混养重量，并且各自排序好后合并为一个数组
            var xxfCounts = xList.Where(p => p.areaId > 4).OrderByDescending(o => o.count).Select(t => (float)t.count).ToArray();
            var xdfCounts = xList.Where(p => p.areaId <= 4).OrderByDescending(o => o.weight).Select(t => t.weight).ToArray();
            var xCounts = xxfCounts.Concat(xdfCounts).ToArray();

            var yxfCounts = yList.Where(p => p.areaId > 4).OrderByDescending(o => o.count).Select(t => (float)t.count).ToArray();
            var ydfCounts = yList.Where(p => p.areaId <= 4).OrderByDescending(o => o.weight).Select(t => t.weight).ToArray();
            var yCounts = yxfCounts.Concat(ydfCounts).ToArray();

            //对成绩(对象鱼数量,混养鱼重量)数组进行逐个比较返回结果,大的在前面
            ret = CompareArray2(xCounts, yCounts);

            return ret;
        }
        public int Compare(Score x, Score y)
        {
            if (x.TotalScore > y.TotalScore)
            {
                return 1;
            }
            else if (x.TotalScore < y.TotalScore)
            {
                return -1;
            }
            else//如果总分相同，则比较对象鱼分数，混养鱼分数，对象鱼数量及混养鱼重量。
            {
                return CompareByPlayerId(x.playerId, y.playerId);
            }
        }
    }*/

    //自定义排序类
    public class ScoreCompare : IComparer<Score>
    {
        private int count = 4;

        public ScoreCompare(int matchCount = 4)
        {
            count = matchCount;
        }

        /// <summary>
        /// Compares two float arrays and determines their relative order.
        /// </summary>
        /// <param name="a">The first float array.</param>
        /// <param name="b">The second float array.</param>
        /// <returns>1 if a is greater than b, -1 if a is less than b, 0 if they are equal.</returns>
        private int CompareArray(float[] a, float[] b)
        {
            int i = a.Length;
            for (int j = 0; j < i; j++)
            {
                if (a[j] > b[j])
                    return 1;
                else if (a[j] < b[j])
                    return -1;
            }
            return 0;
        }

        private int CompareArray2(float[] a, float[] b)
        {
            int i = a.Length;
            for (int j = 0; j < i; j++)
            {
                if (a[j] > b[j])
                    return -1;
                else if (a[j] < b[j])
                    return 1;
            }
            return 0;
        }

        private int CompareScore(Score x, Score y)
        {
            List<float> xXiaoScores = new List<float>();//第一个人对象鱼分数
            List<float> xDaScores = new List<float>();//第一个人混养鱼分数
            List<float> xXiaoFishes = new List<float>();//第一个人对象鱼数量
            List<float> xDaFishes = new List<float>();//第一个人混养鱼重量

            List<float> yXiaoScores = new List<float>();//第二个人对象鱼分数
            List<float> yDaScores = new List<float>();//第二个人混养鱼分数
            List<float> yXiaoFishes = new List<float>();//第二个人对象鱼数量
            List<float> yDaFishes = new List<float>();//第二个人混养鱼重量

            if (x.AreaId1 > 4) //第一场比赛
            {
                xXiaoScores.Add(x.Score1);
                xXiaoFishes.Add(x.Fish1);
            }
            else if (x.AreaId1 <= 4 && x.AreaId1 != 0)
            {
                xDaScores.Add(x.Score1);
                xDaFishes.Add(x.Fish1);
            }

            if (count > 1)//如果比两场比赛
            {
                if (x.AreaId2 > 4)
                {
                    xXiaoScores.Add(x.Score2);
                    xXiaoFishes.Add(x.Fish2);
                }
                else if (x.AreaId2 <= 4 && x.AreaId2 != 0)
                {
                    xDaScores.Add(x.Score2);
                    xDaFishes.Add(x.Fish2);
                }
            }

            if (count > 2)//如果比三场比赛
            {
                if (x.AreaId3 > 4)
                {
                    xXiaoScores.Add(x.Score3);
                    xXiaoFishes.Add(x.Fish3);
                }
                else if (x.AreaId3 <= 4 && x.AreaId3 != 0)
                {
                    xDaScores.Add(x.Score3);
                    xDaFishes.Add(x.Fish3);
                }
            }

            if (count > 3)//如果比四场比赛
            {
                if (x.AreaId4 > 4)
                {
                    xXiaoScores.Add(x.Score4);
                    xXiaoFishes.Add(x.Fish4);
                }
                else if (x.AreaId4 <= 4 && x.AreaId4 != 0)
                {
                    xDaScores.Add(x.Score4);
                    xDaFishes.Add(x.Fish4);
                }
            }

            if (y.AreaId1 > 4)
            {
                yXiaoScores.Add(y.Score1);
                yXiaoFishes.Add(y.Fish1);
            }
            else if (y.AreaId1 <= 4 && y.AreaId1 != 0)
            {
                yDaScores.Add(y.Score1);
                yDaFishes.Add(y.Fish1);
            }

            if (count > 1)
            {
                if (y.AreaId2 > 4)
                {
                    yXiaoScores.Add(y.Score2);
                    yXiaoFishes.Add(y.Fish2);
                }
                else if (y.AreaId2 <= 4 && y.AreaId2 != 0)
                {
                    yDaScores.Add(y.Score2);
                    yDaFishes.Add(y.Fish2);
                }
            }

            if (count > 2)
            {
                if (y.AreaId3 > 4)
                {
                    yXiaoScores.Add(y.Score3);
                    yXiaoFishes.Add(y.Fish3);
                }
                else if (y.AreaId3 <= 4 && y.AreaId3 != 0)
                {
                    yDaScores.Add(y.Score3);
                    yDaFishes.Add(y.Fish3);
                }
            }

            if (count > 3)
            {
                if (y.AreaId4 > 4)
                {
                    yXiaoScores.Add(y.Score4);
                    yXiaoFishes.Add(y.Fish4);
                }
                else if (y.AreaId4 <= 4 && y.AreaId4 != 0)
                {
                    yDaScores.Add(y.Score4);
                    yDaFishes.Add(y.Fish4);
                }
            }

            //对象鱼分数排序后，混养分数排序后合并为一个数组
            float[] xScores = xXiaoScores.OrderBy(n => n).Concat(xDaScores.OrderBy(n => n)).ToArray();
            //对象鱼数量排序后，混养重量排序后合并为一个数组
            float[] xFishes = xXiaoFishes.OrderByDescending(n => n).Concat(xDaFishes.OrderByDescending(n => n)).ToArray();
            //对象鱼分数排序后，混养分数排序后合并为一个数组
            float[] yScores = yXiaoScores.OrderBy(n => n).Concat(yDaScores.OrderBy(n => n)).ToArray();
            //对象鱼数量排序后，混养重量排序后合并为一个数组
            float[] yFishes = yXiaoFishes.OrderByDescending(n => n).Concat(yDaFishes.OrderByDescending(n => n)).ToArray();

            //对分数数组进行逐个比较返回结果,小的在前面
            int ret = CompareArray(xScores, yScores);
            if (ret != 0)
                return ret;

            //对成绩(对象鱼数量,混养鱼重量)数组进行逐个比较返回结果,大的在前面
            ret = CompareArray2(xFishes, yFishes);
            return ret;
        }

        public int Compare(Score x, Score y)
        {
            if (x.TotalScore > y.TotalScore)
            {
                return 1;
            }
            else if (x.TotalScore < y.TotalScore)
            {
                return -1;
            }
            else//如果总分相同，则比较对象鱼分数，混养鱼分数，对象鱼数量及混养鱼重量。
            {
                return CompareScore(x, y);
            }
        }
    }

    public class TeamScoreCompare : IComparer<TeamRank>
    {
        public int Compare(TeamRank x, TeamRank y)
        {
            if (x.TeamScore > y.TeamScore)
            {
                return 1;
            }
            else if (x.TeamScore < y.TeamScore)
            {
                return -1;
            }
            else//如果总分相同，则比较枚举值，枚举类型都对应一个数值默认从0开始。
            {
                return ComparePlayerScore(x, y);
            }
        }

        //对4位钓手分数排序后进行比较
        public int ComparePlayerScore(TeamRank x, TeamRank y)
        {
            float[] xScores = { x.Play1Score, x.Play2Score, x.Play3Score, x.Play4Score };
            float[] yScores = { y.Play1Score, y.Play2Score, y.Play3Score, y.Play4Score };

            Array.Sort(xScores);
            Array.Sort(yScores);

            for (int i = 0; i < 4; i++)
            {
                if (xScores[i] > yScores[i])
                    return 1;
                else if (xScores[i] < yScores[i])
                    return -1;
            }

            return 0;
        }
    }
}